package com.wilmer.qiniuyun.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.wilmer.aliyun.config.AliyunSSMConfig;
import com.wilmer.qiniuyun.config.OpenQiNiuYunConfig;
import com.wilmer.qiniuyun.config.QiNiuYunConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * 七牛OSS管理工具(私有空间-华北地区)
 * @author hans
 * @data 2024-3-31
 */

@Component
public class QiNiuOssUtil {

    /**
     * 配置文件穿件文件
     */
    @Autowired
    public OpenQiNiuYunConfig config;

    /**
     * 上传管理器
     */
    private UploadManager upload;
    /**
     * 桶管理器（存储空间管理器）
     */
    private BucketManager bucket;

    //在构造方法执行后执行初始化七牛云配置
    @PostConstruct
    public void init() {
        //创建配置对象  zone1为华北地区
        Configuration cfg = new Configuration(Zone.zone1());
        //创建上传管理器
        upload = new UploadManager(cfg);
        //创建存储空间管理器
        bucket = new BucketManager(getAuth(), cfg);
    }

    /**
     * 返回认证器（包含的访问密钥）
     *
     * @return
     */
    private Auth getAuth() {
        return Auth.create(config.getAccess_key(),config.getSecret_key());
    }

    /**
     * 获取令牌对象（服务器返回的授权信息）
     *
     * @return
     */
    private String getToken() {
        return getAuth().uploadToken(config.getBucket());
    }

    private String getUrl(String url){
        return getAuth().privateDownloadUrl(url,30000);
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    public String upload(File file, String key) {
        try {
            return upload(new FileInputStream(file), key);
        } catch (FileNotFoundException | QiniuException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 上传文件
     *
     * @param is
     * @param key
     * @return
     * @throws QiniuException
     */
    public String upload(InputStream is, String key) throws QiniuException {
        //上传流
//        key="2023/"+key;
        Response response = upload.put(is, key, getToken(), null, null);
        //解析返回结果
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        //将文件的访问地址返回
        return config.getBase_url() + putRet.key;
    }

    public String uploadBt(byte[] is, String key) throws QiniuException {
        //上传流
       //  key="2024/"+key;
        Response response = upload.put(is, key, getToken());
        //解析返回结果
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        //将文件的访问地址返回
        Auth auth = getAuth();
        return config.getBase_url() + putRet.key;
    }

    /**
     * 删除文件
     *
     * @param key
     */
    public void delete(String key) {
        try {
            bucket.delete(config.getBucket(), key);
        } catch (QiniuException e) {
            e.printStackTrace();
        }
    }
}